$(function () {
    $("#jqGrid").jqGrid({
        url: '../socialsbsummary/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '社保交付明细id', name: 'detailid', index: 'detailid', width: 100 ,hidden:true},
            { label: '批次号', name: 'batchcode', index: 'batchcode', width: 100 },
			{ label: '社保险种', name: 'insuredtype', index: 'insuredtype', width: 80,formatter: getDictInsuredtype},
            { label: '员工姓名', name: 'username', index: 'username', width: 80 },
            { label: '单位id', name: 'unitid', index: 'unitid', width: 80 ,hidden:true},
            { label: '单位名称', name: 'unitname', index: 'unitname', width: 100 },
            { label: '员工身份证', name: 'identitynumber', index: 'identitynumber', width: 100 },
            { label: '实际结算年', name: 'actualsettleyear', index: 'actualsettleyear', width: 100 },
            { label: '实际结算月', name: 'actualsettlemonth', index: 'actualsettlemonth', width: 100 },
			{ label: '单位应付金额', name: 'amountpaypunit', index: 'amountpaypunit', width: 120 },
			{ label: '个人应付金额', name: 'individualamountpay', index: 'individualamountpay', width: 120 },
			{ label: '小计金额', name: 'totalcost', index: 'totalcost', width: 80 },
			{ label: '单位参保比例', name: 'unitinsuredratio', index: 'unitinsuredratio', width: 120 },
			{ label: '个人参保比例', name: 'personinsurratio', index: 'personinsurratio', width: 120 },
			{ label: '本月应付金额单位', name: 'amountpayableunit', index: 'amountpayableunit', width: 160 ,hidden:true},
			{ label: '本月应付金额个人', name: 'amountpayperson', index: 'amountpayperson', width: 160 ,hidden:true},
			{ label: '本月应付金额小计', name: 'amountpaysum', index: 'amountpaysum', width: 160 ,hidden:true},
			{ label: '应缴补报单位', name: 'repairunit', index: 'repairunit', width: 180 },
			{ label: '应缴补报个人', name: 'repairperson', index: 'repairperson', width: 180 },
			{ label: '应缴补报小计', name: 'repairsum', index: 'repairsum', width: 180 },
			{ label: '应缴补差单位', name: 'basechangeunit', index: 'basechangeunit', width: 200 },
			{ label: '应缴补差个人', name: 'basechangeperson', index: 'basechangeperson', width: 200 },
			{ label: '应缴补差小计', name: 'basechangesum', index: 'basechangesum', width: 200 },
			{ label: '单位退款金额', name: 'unitrefund', index: 'unitrefund', width: 120 },
			{ label: '个人退款金额', name: 'personrefund', index: 'personrefund', width: 120 },
			{ label: '退款金额小计', name: 'refundsum', index: 'refundsum', width: 120 },
			{ label: '单位实付金额', name: 'unitpay', index: 'unitpay', width: 120 },
			{ label: '个人实付金额', name: 'personpay', index: 'personpay', width: 120 },
			{ label: '实付金额小计', name: 'paysum', index: 'paysum', width: 120 },
			{ label: '录入日期', name: 'entrytime', index: 'entrytime', width: 80 }, 			
			{ label: '录入人', name: 'inputperson', index: 'inputperson', width: 80 },
			{ label: '付款状态', name: 'paymentstatus', index: 'paymentstatus', width: 80 },
            { label: '一审日期', name: 'firstinstance', index: 'firstinstance', width: 80 },
            { label: '一审人', name: 'firstinstaperson', index: 'firstinstaperson', width: 80 },
            { label: '二审日期', name: 'secondinstance', index: 'secondinstance', width: 80 },
            { label: '二审人', name: 'secondinstperson', index: 'secondinstperson', width: 80 },
            { label: '三审日期', name: 'threeinstance', index: 'threeinstance', width: 80 },
            { label: '三审人', name: 'threeinsteperson', index: 'threeinsteperson', width: 80 },
            { label: '审核状态', name: 'instastatus', index: 'instastatus', width: 80 ,formatter:getinstastatus,unformat:ungetinstastatus}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        loadComplete:function(r){
            if(r.map!=undefined){
                $("#lastmpeople").val(r.map.lastmpeople);
                $("#thismonthadd").val(r.map.thismonthadd);
                $("#thismreduce").val(r.map.thismreduce);
                $("#thismactual").val(r.map.thismactual);
                $("#paymentbase").val(r.map.paymentbase);
                $("#paymentbasesum").val(r.map.paymentbasesum);
            }
        }
    });
    var r =utils.getDictList("socialSecurity");
    vm.insuredtype = r.socialSecurity;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            actualsettleyear: null,
            actualsettlemonth: null,
            insuredtype: null,
            batchcode:null
        },
		showList: true,
		title: null,
		socialsbSummary: {},
        insuredtype:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.socialsbSummary = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.instastatus!='0'){
                alert("审批中的数据不允许修改！");
                return;
            }
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.socialsbSummary.id == null ? "../socialsbsummary/save" : "../socialsbsummary/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.socialsbSummary),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
            for(var i=0;i<ids.length;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.instastatus!="0"&&rowData.instastatus!=""&&rowData.instastatus!=null){
                    alert('选中记录中有已审批的数据!')
                    return;
                }
            }
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../socialsbsummary/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../socialsbsummary/info/"+id, function(r){
                vm.socialsbSummary = r.socialsbSummary;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    actualsettleyear: vm.q.actualsettleyear,
                    actualsettlemonth: vm.q.actualsettlemonth,
                    insuredtype: vm.q.insuredtype,
                    batchcode:vm.q.batchcode
                },
                page:page
            }).trigger("reloadGrid");
		},
        addSocial: function(){
            utils.openDialogView('社保付款',$("#window"),'400px','200px',1);
        },
        firstaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.instastatus!='0'){
                alert("该条记录已在审批中或者还未到达一审节点！");
                return;
            }
            utils.openAuditView("../socialsbsummary/simpleaudit",1,id,$("#jqGrid"));
        },
        secondaudit:function(){
            var id = getSelectedRow();
            if(id == null){bu
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.instastatus=='1'){
                utils.openAuditView("../socialsbsummary/simpleaudit",2,id,$("#jqGrid"));
            }else{
                alert("该条记录已在审批中或者还未到二审节点！");
                return;
            }

        },
        thirdaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.instastatus=='2'){
                utils.openAuditView("../socialsbsummary/simpleaudit",3,id,$("#jqGrid"));
            }else{
                alert("该条记录已在审批中或者还未到三审节点！");
                return;
            }

        }
	}
});
$("#submit").click(function(){
	var year = $("#year").val();
    var month = $("#month").val();
    var insuredtype = $('#selectInsuredtype option:selected').val()
    if(year==""){
        alert("请选择年!");
        return;
	}
    if(month==""){
        alert("请选择月!");
        return;
    }
    if(insuredtype==""){
        alert("请选择保险类别!");
        return;
    }
   var params = {
       year:year,
       month:month,
       insuredtype:insuredtype
    }
    $.ajax({
        type: "POST",
        url: "../socialsbsummary/insertsocial",
        contentType: "application/json",
        data: JSON.stringify(params),
        dataType:"json",
        async:false,
        cache:false,
        success: function(r){
            if(r.code === 0){
                alert('操作成功', function(index){
                    utils.closeCurrentPage();
                    vm.q.batchcode=year+'_'+month+'_'+insuredtype;
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }

        }
    });
});
$("#close").click(function(){
    utils.closeCurrentPage();
});
function getDictInsuredtype(cellvalue, options, rowObject){
    var html="";
    for(var i=0; i<vm.insuredtype.length; i++){
        if(vm.insuredtype[i].value==cellvalue){
            html=vm.insuredtype[i].label;
        }
        if(html==undefined){
            html="";
        }
    }
    return html;
}
function getinstastatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="一级已审核";
    }else if(cellvalue=="2"){
        html="二级已审核";
    }else if(cellvalue=="3"){
        html="三级已审核";
    }
    return html;
}
function ungetinstastatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="一级已审核"){
        html="1";
    }else if(cellvalue=="二级已审核"){
        html="2";
    }else if(cellvalue=="三级已审核"){
        html="3";
    }
    return html;
}